﻿namespace Amazon.Checkout.Controllers
{
    using System;
    using System.Web.Mvc;
    using Orchard;
    using Orchard.Localization;
    using Orchard.Logging;
    using Orchard.Themes;
    using Orchard.UI.Admin;
    using Orchard.UI.Notify;
    using Orchard.Utility.Extensions;
    using Services;
    using ViewModels;

    [Admin]
    [Themed]
    public class TaxTableController : Controller
    {
        private readonly ICheckoutByAmazonService amazonService;
        private readonly IOrchardServices orchardServices;
        private readonly Localizer T;

        public TaxTableController(ICheckoutByAmazonService amazonService, IOrchardServices services)
        {
            this.amazonService = amazonService;
            this.orchardServices = services;

            this.T = NullLocalizer.Instance;
        }

        [HttpGet]
        public ActionResult List()
        {
            var model = new TaxTableListViewModel { TaxTables = this.amazonService.GetTaxTables() };
            return View(model);
        }

        [HttpGet]
        public ActionResult Delete(int taxTableId)
        {
            var taxTable = this.amazonService.GetTaxTable(taxTableId);

            if (taxTable == null)
            {
                this.orchardServices.Notifier.Error(this.T("Delete process failed: tax table does not exist."));
                return RedirectToAction("List");
            }

            this.amazonService.DeleteTaxTable(taxTableId);

            this.orchardServices.Notifier.Information(this.T("Tax table {0} deleted.", taxTable.Name));

            return RedirectToAction("List");
        }

        [HttpGet]
        public ActionResult Edit(int taxTableId)
        {
            var taxTable = this.amazonService.GetTaxTable(taxTableId);

            var viewModel = new TaxNewUpdateViewModel
            {
                TaxTableId = taxTableId,
                Name = taxTable.Name,
                TaxRuleIds = taxTable.Taxes,
                TaxRules = this.amazonService.GetTaxRules(taxTable.Id)
            };

            return View("NewEdit", viewModel);
        }

        [HttpPost]
        public ActionResult Edit(TaxNewUpdateViewModel viewModel)
        {
            var taxTable = this.amazonService.GetTaxTable(viewModel.TaxTableId);

            if (taxTable == null)
            {
                this.orchardServices.Notifier.Error(this.T("Tax table update failed: The Tax table with the '{1}' identifier does not exists.", viewModel.TaxTableId));
                return View("NewEdit", viewModel);
            }

            this.amazonService.UpdateTaxTable(viewModel.TaxTableId, viewModel.Name, viewModel.TaxRuleIds);
            this.orchardServices.Notifier.Information(this.T("Tax table successfully updated."));

            return RedirectToAction("Edit", viewModel);
        }

        [HttpPost]
        public JsonResult Update(int taxTableId, string name, string taxRuleIds)
        {
            var taxTable = this.amazonService.GetTaxTable(taxTableId);

            if (taxTable == null)
            {
                return this.Json(new { error = this.T("Tax table update failed: The Tax table with the '{0}' identifier does not exists.", taxTableId) });
            }

            this.amazonService.UpdateTaxTable(taxTableId, name, taxRuleIds);

            return this.Json(taxTable);
        }

        [HttpGet]
        public ActionResult New()
        {
            return View("NewEdit", new TaxNewUpdateViewModel());
        }

        [HttpPost]
        public ActionResult New(TaxNewUpdateViewModel viewModel)
        {
            try
            {
                if (!this.TryValidateModel(viewModel)) 
                {
                    return View("NewEdit", viewModel);
                }

                var taxTable = this.amazonService.CreateTaxTable(viewModel.Name, viewModel.TaxRuleIds);

                viewModel.TaxTableId = taxTable.Id;
                this.orchardServices.Notifier.Information(this.T("Tax table successfully created."));

                return View("NewEdit", viewModel);
            }
            catch (Exception ex)
            {
                this.Error(ex, this.T("Creating tax table failed: {0}.", ex.Message), NullLogger.Instance, this.orchardServices.Notifier);
                return View("NewEdit", viewModel);
            }
        }
    }
}